package presentation;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisState;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRendererState;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

public class PChartFactory {
	
	public static JFreeChart createBarChart(String title,int offset,int n)
	{
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		int i = 0;
		for(i=0;i<n;i++)
		{
			dataset.setValue(0.0,"Cell Voltage",String.format("Cell %d",offset+i));
		}
		
		JFreeChart chart = ChartFactory.createBarChart(title, "Cell number", "Cell Voltage", dataset, PlotOrientation.VERTICAL, false, true, false);
		
		chart.setBackgroundPaint(Color.white);
		
		CategoryPlot plot = (CategoryPlot)chart.getPlot();
		
		plot.setBackgroundPaint(Color.white);
		
		BarRenderer d = (BarRenderer)plot.getRenderer();
		
		d.setBarPainter(new StandardBarPainter());
		
		
		d.setDrawBarOutline(false);
		d.setShadowVisible(false);
		
		
		NumberAxis range = (NumberAxis) plot.getRangeAxis();
        range.setRange(3.0, 3.7);
        
		
		CategoryAxis xAxis = (CategoryAxis)plot.getDomainAxis();
		
		xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
		

		
		
		return chart;
	}
	
	public static JFreeChart createFixedXYGraph(String xLabel, String yLabel, double[] boundary, int yTickUnit) {
		XYSeries dataSeries = new XYSeries("Speed");
		dataSeries.setMaximumItemCount(100);
        
		XYSeriesCollection dataset = new XYSeriesCollection(dataSeries);
		
		JFreeChart chart = ChartFactory.createXYLineChart(
	            null,       // chart title
	            xLabel,                    // domain axis label
	            yLabel,                   // range axis label
	            dataset,                   // data
	            PlotOrientation.VERTICAL,  // orientation
	            false,                      // include legend
	            true,                      // tooltips
	            false                      // urls
	        );
		
		chart.setPadding(new RectangleInsets(10,0,0,0));
		
		chart.setBackgroundPaint(Color.white);
	        
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setRangeGridlinePaint(Color.gray);
		
	    // Axes
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setAutoRange(false);
		rangeAxis.setRange(boundary[0],boundary[1]);
		rangeAxis.setTickUnit(new NumberTickUnit(yTickUnit));
		
		NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
		domainAxis.setAutoRange(true);
		domainAxis.setAutoRangeMinimumSize(100);
		//domainAxis.setTickUnit(new NumberTickUnit(10));
	        
	    // Line design
		XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
		renderer.setSeriesPaint(0, Color.green);
		renderer.setSeriesStroke(
				0, new BasicStroke(
					2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
					1.0f
				)
			);
	        
		return chart;
	}
	
	public static JFreeChart createXYGraph(String xLabel, String yLabel, int samples, Color color) {
		XYSeries dataSeries = new XYSeries("Speed");
		dataSeries.setMaximumItemCount(samples);
		
		XYSeriesCollection dataset = new XYSeriesCollection(dataSeries);
		
		JFreeChart chart = ChartFactory.createXYLineChart(
	            null,       // chart title
	            xLabel,                    // domain axis label
	            yLabel,                   // range axis label
	            dataset,                   // data
	            PlotOrientation.VERTICAL,  // orientation
	            false,                      // include legend
	            true,                      // tooltips
	            false                      // urls
	        );
		
		//chart.setPadding(new RectangleInsets(10,0,0,0));
		
		chart.setBackgroundPaint(Color.white);
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setRangeGridlinePaint(Color.gray);
		
	    // Axes
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setAutoRange(true);
		
		NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
		domainAxis.setAutoRange(true);
		domainAxis.setFixedAutoRange(samples);
	        
	    // Line design
		XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
		renderer.setSeriesPaint(0, color);
		renderer.setSeriesStroke(
				0, new BasicStroke(
					2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
					1.0f
				)
			);
	        
		return chart;
	}
	
	public static void setRangeAxis(JFreeChart chart, double[] boundary) {
		if (boundary == null) {
			NumberAxis rangeAxis = ((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis());
			rangeAxis.setAutoRange(true);
		} else {
			NumberAxis rangeAxis = ((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis());
			rangeAxis.setAutoRange(false);
			rangeAxis.setRange(boundary[0],boundary[1]);
		}
	}
	
	public static void setRangeAxisLabel(JFreeChart chart, String label) {
		// Chart is XY chart:
		if (chart.getPlot().getClass().equals((new XYPlot()).getClass())) {
			chart.getXYPlot().getRangeAxis().setLabel(label);
		}
	}
	
	public static void setSampleCount(JFreeChart chart, int sampleCount) {
		// Chart is XY chart:
		if (chart.getPlot().getClass().equals((new XYPlot()).getClass())) {
			((XYSeriesCollection) chart.getXYPlot().getDataset()).getSeries(0).setMaximumItemCount(sampleCount);
			chart.getXYPlot().getDomainAxis().setFixedAutoRange(sampleCount);
		}
	}
	
	public static void clearSamples(JFreeChart chart) {
		// Chart is XY chart:
		if (chart.getPlot().getClass().equals((new XYPlot()).getClass())) {
			((XYSeriesCollection) chart.getXYPlot().getDataset()).getSeries(0).clear();
		}
	}
	
	public static void addSample(JFreeChart chart, Number sample) {
		// Chart is XY chart:
		if (chart.getPlot().getClass().equals((new XYPlot()).getClass())) {
			XYSeries chartDataSeries = ((XYSeriesCollection) chart.getXYPlot().getDataset()).getSeries(0);
			int xValue = (int) chartDataSeries.getMaxX() + 1;
			chartDataSeries.add(xValue, sample);
			
		}
	}
	
	public static void updateBarChart(JFreeChart chart,double[] voltages,int n,int offset)
	{
		DefaultCategoryDataset dataset = (DefaultCategoryDataset) chart.getCategoryPlot().getDataset();
		int i = 0;
		for (i=0;i<n;i++)
		{
			dataset.addValue(voltages[i], "Cell Voltage", String.format("Cell %d",offset+i));
		}
		
	}
}
